# coding:utf-8
from flask import Flask, request, jsonify
from . import blue_api

tasks = [
	{
		'id': 1,
		'title': u'Buy groceries',
		'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
		'done': False
	},
	{
		'id': 2,
		'title': u'Learn Python',
		'description': u'Need to find a good Python tutorial on the web',
		'done': False
	}
]

from flask import url_for


@blue_api.route('/v1.0/get_tasks/<task_id>', methods=['GET'])
def get_tasks(task_id):   ##查看某个 tasks信息 http://127.0.0.1:8000/api/v1.0/get_tasks/1
	task = filter(lambda t: t['id'] == int(task_id), tasks)
	return jsonify({'tasks': task[0]})


class make_api_urlpath(object):
	def make_public_task(self,task):    ##生成 URL path
		new_task = {}
		for key in task:
			if key == 'id':
				new_task['uri'] = url_for('blue_api.get_tasks', task_id=task['id'], _external=True)
			else:
				new_task[key] = task[key]
		return new_task

	def make_public_db(self,db):
		pass

@blue_api.route('/v1.0/get_tasks_urlpath/', methods=['GET'])  ##访问 http://127.0.0.1:8000/api/v1.0/get_tasks_urlpath/ 获得tasks url列表
def get_tasks_urlpath():
	m = make_api_urlpath()
	return jsonify({'tasks': map(m.make_public_task, tasks)})



@blue_api.route('/v1.0/', methods=["GET", "POST", 'DELETE', 'PUT'])
def ex_tasks():
	print request.method
	if request.method == 'PUT':
		print request.json    ##获得上传上来的 json
		print 'update db'
	if request.method == 'delete':
		print 'delete db'
	if request.method == 'POST':
		print 'query db'

	return jsonify({'tasks': tasks})
